package com.teangtang.fly.controller;

import com.teangtang.fly.dto.CityDto;
import com.teangtang.fly.entity.City;
import com.teangtang.fly.entity.Province;
import com.teangtang.fly.get.CityWithProvince;
import com.teangtang.fly.service.CityService;
import com.teangtang.fly.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/")
public class ProvinceCityController {

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    // Province Endpoints
    @PostMapping("/provinces")
    public ResponseEntity<Province> createProvince(@RequestBody Province province) {
        Province savedProvince = provinceService.saveProvince(province);
        return new ResponseEntity<>(savedProvince, HttpStatus.CREATED);
    }

    @GetMapping("/provinces")
    public ResponseEntity<List<Province>> getAllProvinces() {
        List<Province> provinces = provinceService.findAllProvinces();
        return new ResponseEntity<>(provinces, HttpStatus.OK);
    }

    @GetMapping("/provinces/{provinceCode}")
    public ResponseEntity<Province> getProvinceById(@PathVariable Long provinceCode) {
        Province province = provinceService.findProvinceById(provinceCode);
        if (province != null) {
            return new ResponseEntity<>(province, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/provinces/{provinceCode}")
    public ResponseEntity<Void> deleteProvince(@PathVariable Long provinceCode) {
        provinceService.deleteProvince(provinceCode);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    // City Endpoints
    @PostMapping("/cities")
    public ResponseEntity<CityDto> createCity(@RequestBody CityDto cityDto) {
        Province province = provinceService.findProvinceById(cityDto.getProvinceCode());
        if (province == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND); // Or handle the error differently
        }

        City city = new City();
        city.setCityName(cityDto.getCityName());
        city.setProvince(province);
        List<City> cities = province.getCities();
        cities.add(city);
        province.setCities(cities);
        Province savedProvince = provinceService.saveProvince(province);
        return new ResponseEntity<>(cityDto, HttpStatus.CREATED);
    }


    @GetMapping("/cities")
    public ResponseEntity<List<CityWithProvince>> getAllCities() {
        List<City> cities = cityService.findAllCities();
        List<CityWithProvince> cityWithProvinces = new ArrayList<>();
        for (City city : cities) {
            CityWithProvince cityWithProvince = new CityWithProvince(city, city.getProvince());
            cityWithProvinces.add(cityWithProvince);
        }
        return new ResponseEntity<>(cityWithProvinces, HttpStatus.OK);
    }
    @GetMapping("/cities/{cityCode}")
    public ResponseEntity<CityWithProvince> getCityById(@PathVariable Long cityCode) {
        City city = cityService.findCityByCityCode(cityCode);
        if (city != null) {
            CityWithProvince cityWithProvince = new CityWithProvince(city, city.getProvince());
            return new ResponseEntity<>(cityWithProvince, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @DeleteMapping("/cities/{cityCode}")
    public ResponseEntity<Void> deleteCity(@PathVariable Long cityCode) {
        cityService.deleteCity(cityCode);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}